<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> 
</head>

<body>


<script type="text/javascript">
	$(function(){
 
		/*interval = window.setInterval(float,  50);*/
		
		
		// 添加Jquery扩展的写法
		$.extend({
			float:function(){
				_width = $(window).width()-$('#box').width();
				_top = $(window).height()-$('#box').height();
				
				if($('#box').offset().top == 0){
					$('#box').css({left:$('#box').offset().left-1,top:$('#box').offset().top+1});
				}
				else{
					$('#box').css({left:$('#box').offset().left-1,top:$('#box').offset().top-1});
				}
			},
			
			/*出现a到b的随机数*/
			rand:function(a, b){
				var range = b - a;  
				return(a + Math.round(range * Math.random()));  
			}
			
			
			
		})
		 
		var scrollX = self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || document.body.scrollLeft;
	
		//alert($.rand(0,100));   $('#box').offset().top
		
		$('#box').css({"left": $.rand(0,$(window).width()-$('#box').width()), "top": $.rand(0,$(window).height()-$('#box').height())});
		
		
		setInterval("$.float()",50);
		
		
		
	})
	
</script>


<style type="text/css">
	.box {
		width:100px; height:100px; background:red; position:absolute; top:343px; left:234px;
}
</style>


<div class="box" id="box"></div>

<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

</body>
</html>

